layui.use(['jquery', 'febs', 'table', 'form', 'fileUpload', 'commonJS', 'formSelects', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        formSelects = layui.formSelects,
        $view = $('#task-edit'),
        fileUpload = layui.fileUpload,
        commonJS = layui.commonJS,
        validate = layui.validate;
        form.verify(validate);
        formSelects.render();
        form.render();
         initFormValue();
         initModels();
        initfile();

function initFormValue() {
    if (scheduleData) {
        form.val("task-edit-form", {
            "taskCode": scheduleData.taskCode,
            "projectCode": scheduleData.projectCode,
            "modelids": scheduleData.modelids,
            "taskType": scheduleData.taskType,
            "status": scheduleData.status,
            "testDay": scheduleData.testDay,
            "iskeyTask": scheduleData.iskeyTask,
            "remark": scheduleData.remark,
            "planStartdate": scheduleData.planStartdate,
            "planStarttime": scheduleData.planStarttime,
            "planEnddate": scheduleData.planEnddate,
            "planEndtime": scheduleData.planEndtime,
            "actuallyStartdate": scheduleData.actuallyStartdate,
            "actuallyEnddate": scheduleData.actuallyEnddate,
            "createdTime": scheduleData.createdTime,
            "rsid": scheduleData.rsid,
        });
    }

}

    function initfile() {
        fileUpload.render({
            elem: '#fileUploadBtn',
            url: ctx + 'task/upload',
            type: 2,//0-图片，1-视频，2-其他
            maxNum: 5,
            multiple: true,
            defaultData: scheduleData && scheduleData.fileList ? scheduleData.fileList : [],
            isDetail: true,
            done: function (res) {
            },
            error: function () {
                layer.msg("服务器异常");
            }
        });
    }

    form.on('submit(task-edit-form-save)', function (data) {
        commitSchedule(data);
        return false;
    });


    function commitSchedule(data) {
        data.field.voucherId=data.field.taskCode;
        data.field.status=data.field.auditStatus;
        data.field.remark=data.field.auditRemark;
        febs.modal.confirm("保存提示！", "是否保存？", function (index) {
                febs.postArray(ctx + 'schedule/audit', data.field, function () {
                    layer.closeAll();
                    $('#febs-task').find('#query').trigger('click');
                });
        });
    }


    function initModels() {
        febs.table.init({
            elem: $view.find('table'),
            id: 'taskModels',
            data: scheduleData.taskModelArray,
            page:false,
            loading:false,
            even:false,
            format: 'yyyy-MM-dd HH:mm',
            height: getTableHeight()/3,
            width: 580,
            cols: [[
                {field: 'id', title: 'id', minWidth: 20,hide:true},
                {field: 'typename', title: '模型', width: 30,edit:false,sort:false},
                {field: 'code', title: '模型编号', width: 180,edit:false,templet:function (d){
                        return    d.code.slice(3);
                    }},
                {field: 'demandEnddate',  title: '要求完成时间', width: 150,edit:false,templet: function(d) {
                        if (d.demandEnddate == undefined) {
                            return "";
                        } else {
                            return layui.util.toDateString(d.demandEnddate, "yyyy-MM-dd HH:mm");
                        }
                    }
                        },
                {field: 'actuallyEnddate', title: '实际完成时间', width: 150,edit:false,templet: function (d){
                        if(d.actuallyEnddate==undefined){
                            return "";
                        }else {
                            return layui.util.toDateString(d.actuallyEnddate, "yyyy-MM-dd HH:mm");
                        }
                     } },
            ]],
            // toolbar: '#toolbarDemo',
            defaultToolbar: [],
            done: function(res, curr, count){
            }
        });
    }



    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

})
;